package com.winit.openapi.ums.controller;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.winit.common.query.Page;
import com.winit.common.spi.context.CommandContext;
import com.winit.erp.spi.bpartner.BpartnerService;
import com.winit.erp.spi.bpartner.command.BpartnerCommand;
import com.winit.erp.spi.bpartner.vo.Bpartner;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.FastJSONUtils;
import com.winit.ums.spi.finance.FinanceSupplierService;
import com.winit.ums.spi.finance.command.CreateOrModifyFinanceSupplierCommand;
import com.winit.ums.spi.finance.command.FinanceSupplierApplyCommand;
import com.winit.ums.spi.finance.command.QuerySupplierApplicationCommand;
import com.winit.ums.spi.finance.vo.FinanceSupplierApplyVo;
import com.winit.ums.spi.finance.vo.FinanceSupplierPageVo;
import com.winit.uworkflow.spi.WorkflowService;

/**
 * 金融供应商Controller
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * hongguo.yan_guest 	1.0  		2016年7月22日 	Created
 *
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "ums/finance/supplier")
public class FinanceSupplierController extends BaseController {

    private Logger                 logger = LoggerFactory.getLogger(EorIorInfoController.class);
    @Resource
    private BpartnerService        bpartnerService;
    @Resource
    private FinanceSupplierService financeSupplierService;
    @Resource
    private WorkflowService        workflowService;

    /**
     * 分页查询供应商列表接口
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/queryListByPage")
    public String querySupplierListByPage(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        QuerySupplierApplicationCommand supplierApplicationCommand = FastJSONUtils.toJavaObject(requestMsg.getData(),
            QuerySupplierApplicationCommand.class);
        if (supplierApplicationCommand == null) {
            supplierApplicationCommand = new QuerySupplierApplicationCommand();
        }
        String userId = CommandContext.getContext().getUserId();
        BpartnerCommand bpartnerCommand = new BpartnerCommand();
        bpartnerCommand.setcBpartnerId(Long.valueOf(userId));
        Bpartner bpartner = bpartnerService.queryBpartnerById(bpartnerCommand);
        if (bpartner == null) {
            logger.info("根据客户id:{}查询客户code为空", userId);
            return ERROR;
        }
        supplierApplicationCommand.setCustomerCode(bpartner.getValue());
        supplierApplicationCommand.setCtx(CommandContext.getContext());
        Page<FinanceSupplierPageVo> financeSupplierPageVo = financeSupplierService.queryFinanceSupplierListPage(supplierApplicationCommand);
        responseMsg.setData(financeSupplierPageVo);
        return SUCCESS;

    }
    
    /**
     * 供应商详情接口
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/queryDetail")
    public String querySupplierDetail(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        FinanceSupplierApplyCommand supplierApplicationCommand = FastJSONUtils.toJavaObject(requestMsg.getData(),
            FinanceSupplierApplyCommand.class);
        if (supplierApplicationCommand == null) {
            supplierApplicationCommand = new FinanceSupplierApplyCommand();
        }
        String userId = CommandContext.getContext().getUserId();
        BpartnerCommand bpartnerCommand = new BpartnerCommand();
        bpartnerCommand.setcBpartnerId(Long.valueOf(userId));
        Bpartner bpartner = bpartnerService.queryBpartnerById(bpartnerCommand);
        if (bpartner == null) {
            logger.info("根据客户id:{}查询客户code为空", userId);
            return ERROR;
        }
        supplierApplicationCommand.setCustomerCode(bpartner.getValue());
        supplierApplicationCommand.setCtx(CommandContext.getContext());
        FinanceSupplierApplyVo financeSupplierPageVo = financeSupplierService.selectFspSupplierByApplyId(supplierApplicationCommand);
        responseMsg.setData(financeSupplierPageVo);
        return SUCCESS;

    }
    

    /**
     * 校验供应商是否在黑明单，重复性，一致性
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/check")
    public String checkSupplierPoint(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        CreateOrModifyFinanceSupplierCommand supplierCommand = FastJSONUtils.toJavaObject(requestMsg.getData(),
            CreateOrModifyFinanceSupplierCommand.class);
        if (supplierCommand == null) {
            supplierCommand = new CreateOrModifyFinanceSupplierCommand();
        }
        String userId = CommandContext.getContext().getUserId();
        BpartnerCommand bpartnerCommand = new BpartnerCommand();
        bpartnerCommand.setcBpartnerId(Long.valueOf(userId));
        Bpartner bpartner = bpartnerService.queryBpartnerById(bpartnerCommand);
        if (bpartner == null) {
            logger.info("根据客户id:{}查询客户code为空", userId);
            return ERROR;
        }
        supplierCommand.setCustomerCode(bpartner.getValue());
        supplierCommand.setCtx(CommandContext.getContext());
        String retval = financeSupplierService.checkSupplier(supplierCommand);
        responseMsg.setData((Object)retval);
        return SUCCESS;
    }

    /**
     * 保存草稿供应商
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/save")
    public String saveCustomerSupplier(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        logger.info("getData="+requestMsg.getData().toString());
        CreateOrModifyFinanceSupplierCommand supplierCommand = FastJSONUtils.toJavaObject(requestMsg.getData(),
            CreateOrModifyFinanceSupplierCommand.class);
        if (supplierCommand == null) {
            supplierCommand = new CreateOrModifyFinanceSupplierCommand();
        }
        logger.info("保存草稿供应商参数："+supplierCommand.toString());
        
        String userId = CommandContext.getContext().getUserId();
        BpartnerCommand bpartnerCommand = new BpartnerCommand();
        bpartnerCommand.setcBpartnerId(Long.valueOf(userId));
        Bpartner bpartner = bpartnerService.queryBpartnerById(bpartnerCommand);
        if (bpartner == null) {
            logger.info("根据客户id:{}查询客户code为空", userId);
            return ERROR;
        }
        supplierCommand.setCustomerCode(bpartner.getValue());
        supplierCommand.setCtx(CommandContext.getContext());
        financeSupplierService.createSupplier(supplierCommand);
        return SUCCESS;
    }

    /**
     * 提交供应商
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/submit")
    public String commitCustomerSupplier(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        CreateOrModifyFinanceSupplierCommand supplierCommand = FastJSONUtils.toJavaObject(requestMsg.getData(),
            CreateOrModifyFinanceSupplierCommand.class);

        if (supplierCommand == null) {
            supplierCommand = new CreateOrModifyFinanceSupplierCommand();
        }
        String userId = CommandContext.getContext().getUserId();
        BpartnerCommand bpartnerCommand = new BpartnerCommand();
        bpartnerCommand.setcBpartnerId(Long.valueOf(userId));
        Bpartner bpartner = bpartnerService.queryBpartnerById(bpartnerCommand);
        if (bpartner == null) {
            logger.info("根据客户id:{}查询客户code为空", userId);
            return ERROR;
        }
        supplierCommand.setCustomerCode(bpartner.getValue());
        supplierCommand.setCtx(CommandContext.getContext());
        financeSupplierService.commitSupplier(supplierCommand);
        // 调用工作流接口
        // StartFlowInstanceCommand workCommand=new StartFlowInstanceCommand();
        // workflowService.startFlowInstance(workCommand);
        return SUCCESS;
    }
}
